#!/bin/bash

# cjgit - Push Command
# Push all Cangjie projects to myfork or default remote

# Source common functions
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/cjgit-common.sh"

# Function to push specific projects
push_specific_projects() {
    local projects_to_push=("$@")
    local push_failed=false
    
    print_info "Pushing changes to remote for ${#projects_to_push[@]} projects..."
    echo
    
    for project in "${projects_to_push[@]}"; do
        print_info "Pushing project: $project"
        
        # Check if project directory exists
        if [[ ! -d "$project" ]]; then
            print_error "Project directory '$project' not found!"
            push_failed=true
            continue
        fi
        
        # Check if it's a git repository
        if [[ ! -d "$project/.git" ]]; then
            print_error "'$project' is not a git repository!"
            push_failed=true
            continue
        fi
        
        # Check for local changes and warn
        if has_local_changes "$project"; then
            print_warning "Uncommitted changes detected in '$project'! Consider committing before pushing."
        fi
        
        # Get current branch
        current_branch=$(get_current_branch "$project")
        if [[ -z "$current_branch" ]]; then
            print_error "Not on any branch in '$project', skipping..."
            push_failed=true
            continue
        fi
        
        # Determine which remote to push to
        push_remote=$(get_push_remote "$project")
        if [[ -z "$push_remote" ]]; then
            print_error "No push remote found for '$project'. Neither myfork nor default remote is configured."
            push_failed=true
            continue
        fi
        
        print_info "Pushing branch '$current_branch' to remote '$push_remote'"
        
        # Push to the remote
        if git -C "$project" push "$push_remote" "$current_branch"; then
            print_success "Successfully pushed '$current_branch' to '$push_remote' in '$project'"
        else
            print_error "Failed to push '$current_branch' to '$push_remote' in '$project'"
            push_failed=true
        fi
        
        echo
    done
    
    if [[ "$push_failed" == true ]]; then
        print_error "Push operation failed for one or more projects"
        return 1
    else
        print_success "Push operation completed successfully"
        return 0
    fi
}

# Push command implementation
push_command() {
    local force_push=false
    
    # Parse command line options
    while [[ $# -gt 0 ]]; do
        case $1 in
            --force)
                force_push=true
                shift
                ;;
            *)
                print_error "Unknown option: $1"
                echo
                help_command
                exit 1
                ;;
        esac
    done
    
    print_info "Starting push operation for all Cangjie projects..."
    if [[ "$force_push" == true ]]; then
        print_warning "FORCE PUSH enabled - this will overwrite remote history!"
    fi
    echo
    
    for project in "${PROJECTS[@]}"; do
        print_info "Processing project: $project"
        
        # Check if project directory exists
        if [[ ! -d "$project" ]]; then
            print_error "Project directory '$project' not found!"
            continue
        fi
        
        # Check if it's a git repository
        if [[ ! -d "$project/.git" ]]; then
            print_error "'$project' is not a git repository!"
            continue
        fi
        
        # Check for local changes and warn
        if has_local_changes "$project"; then
            print_warning "Uncommitted changes detected in '$project'! Consider committing before pushing."
        fi
        
        # Get current branch
        current_branch=$(get_current_branch "$project")
        if [[ -z "$current_branch" ]]; then
            print_error "Not on any branch in '$project', skipping..."
            continue
        fi
        
        # Determine which remote to push to
        push_remote=$(get_push_remote "$project")
        if [[ -z "$push_remote" ]]; then
            print_error "No push remote found for '$project'. Neither myfork nor default remote is configured."
            continue
        fi
        
        # Build push command
        local push_cmd="git -C '$project' push"
        if [[ "$force_push" == true ]]; then
            push_cmd="$push_cmd --force"
            print_warning "Force pushing branch '$current_branch' to remote '$push_remote'"
        else
            print_info "Pushing branch '$current_branch' to remote '$push_remote'"
        fi
        
        push_cmd="$push_cmd '$push_remote' '$current_branch'"
        
        # Push to the remote
        if eval "$push_cmd"; then
            if [[ "$force_push" == true ]]; then
                print_success "Successfully force pushed '$current_branch' to '$push_remote' in '$project'"
            else
                print_success "Successfully pushed '$current_branch' to '$push_remote' in '$project'"
            fi
        else
            if [[ "$force_push" == true ]]; then
                print_error "Failed to force push '$current_branch' to '$push_remote' in '$project'"
            else
                print_error "Failed to push '$current_branch' to '$push_remote' in '$project'"
            fi
        fi
        
        echo
    done
    
    if [[ "$force_push" == true ]]; then
        print_success "Force push operation completed"
    else
        print_success "Push operation completed"
    fi
}

# Help function for push command
help_command() {
    echo "Usage: cjgit push [OPTIONS]"
    echo
    echo "Push all Cangjie projects to myfork or default remote"
    echo
    echo "Options:"
    echo "  --force           Force push (overwrites remote history)"
    echo
    echo "Examples:"
    echo "  cjgit push                     # Push all projects to myfork or default remote"
    echo "  cjgit push --force             # Force push all projects"
}

# Main execution
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
    help_command
    exit 0
fi

push_command "$@"